﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Graphe.Collations;
using Graphe.Windows.Dialogs;

namespace Graphe.Windows.Controls.Collations
{
    public class VariantsControl : StackPanel
    {
        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public VariantsControl()
        {
            Loaded += new RoutedEventHandler(VariantsControl_Loaded);
        }

        #endregion
        #region Controls

        private StackPanel _stackPanelVariantsHeader;
        private TextBlock _textBlockHeader;
        private Button _buttonAddVariant;
        private StackPanel _stackPanelVariants;

        #endregion
        #region Private Instance Fields

        private bool _buttonsAreShowing;

        #endregion
        #region Public Properties

        public bool ControlIsInitialized { get; set; }

        #endregion

        private void VariantsControl_Loaded(object sender, RoutedEventArgs e)
        {
            MouseEnter += new System.Windows.Input.MouseEventHandler(VariantsControl_MouseEnter);
            MouseLeave += new System.Windows.Input.MouseEventHandler(VariantsControl_MouseLeave);

            Style = Application.Current.FindResource("LightBackground") as Style;

        }

        private void VariantsControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!_buttonsAreShowing)
                return;

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromSeconds(.3));
            animation.From = .7;
            animation.To = 0;

            _buttonAddVariant.BeginAnimation(Button.OpacityProperty, animation);

            _buttonsAreShowing = false;
        }

        private void VariantsControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_buttonsAreShowing)
                return;

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromSeconds(.5));
            animation.From = 0;
            animation.To = .7;

            _buttonAddVariant.BeginAnimation(Button.OpacityProperty, animation);

            _buttonsAreShowing = true;
        }

        private void Initialize()
        {
            if (ControlIsInitialized)
                return;

            ControlIsInitialized = true;

            _stackPanelVariantsHeader = new StackPanel();
            _stackPanelVariantsHeader.Style = CollationsControlStylePicker.FindProperVersionResource("VariantsHeaderPanelStyle") as Style;
            Children.Add(_stackPanelVariantsHeader);

            _textBlockHeader = new TextBlock();
            _textBlockHeader.Style = CollationsControlStylePicker.FindProperVersionResource("VariantsHeaderText") as Style;
            Run headerRun = new Run("Variants:");
            _textBlockHeader.Inlines.Add(headerRun);

            _buttonAddVariant = new Button();
            _buttonAddVariant.Content = "+";
            _buttonAddVariant.Click += new RoutedEventHandler(_buttonAddVariant_Click);
            _buttonAddVariant.ToolTip = "Click this button to add a variant.";
            _buttonAddVariant.Style = CollationsControlStylePicker.FindProperVersionResource("AddVariantButton") as Style;
            _buttonAddVariant.Template = CollationsControlStylePicker.FindProperVersionResource("ThemedButton") as ControlTemplate;

            _stackPanelVariantsHeader.Children.Add(_textBlockHeader);
            _stackPanelVariantsHeader.Children.Add(_buttonAddVariant);

            _stackPanelVariants = new StackPanel();
            _stackPanelVariants.Style = CollationsControlStylePicker.FindProperVersionResource("VariantsStackPanelStyle") as Style;
            Children.Add(_stackPanelVariants);
        }

        public Verse Verse { get; set; }

        public void Render()
        {
            Initialize();

            _stackPanelVariants.Children.Clear();

            foreach (Variant variant in Verse.Variants)
            {
                StackPanel stackPanelVariant = new StackPanel();
                _stackPanelVariants.Children.Add(stackPanelVariant);
                stackPanelVariant.Orientation = Orientation.Horizontal;

                TextBlock textBlockBaseText = new TextBlock();
                textBlockBaseText.Text = variant.BaseText + " ] ";
                textBlockBaseText.Style = CollationsControlStylePicker.FindProperVersionResource("VariantText") as Style;
                stackPanelVariant.Children.Add(textBlockBaseText);
                
                for (int i = 0; i <= variant.Readings.Count - 1; i++)
                {
                    TextBlock textBlockReadingText = new TextBlock();
                    textBlockReadingText.Style = CollationsControlStylePicker.FindProperVersionResource("RegularUnicodeCollationText") as Style;

                    if (i > 0)
                        textBlockReadingText.Text += " // ";

                    textBlockReadingText.Text += variant.Readings[i].Text + " ";
                    stackPanelVariant.Children.Add(textBlockReadingText);

                    ManuscriptListControl manuscriptListControl = new ManuscriptListControl();
                    manuscriptListControl.Manuscripts = variant.Readings[i].Manuscripts;
                    manuscriptListControl.Render();
                    stackPanelVariant.Children.Add(manuscriptListControl);
                }
            }
        }

        private void _buttonAddVariant_Click(object sender, RoutedEventArgs e)
        {
            EditVariantDialog window = new EditVariantDialog();
            window.VerseText = Verse.Text;


            if (true == (bool)window.ShowDialog())
            {

                Variant variant = new Variant();

                variant.BaseText = window.BaseText;

                Reading reading = new Reading();
                reading.Text = window.ReadingText;



                switch (reading.Text)
                {
                    case "addition":
                        reading.Type = EVariantType.Addition;
                        break;
                    case "incipit":
                        reading.Type = EVariantType.Incipit;
                        break;
                    case "omission":
                        reading.Type = EVariantType.Omission;
                        break;
                    default:
                        reading.Type = EVariantType.NotSpecified;
                        break;
                }


                Graphe.Collations.Manuscript manuscript = new Graphe.Collations.Manuscript();
                manuscript.Symbol = window.Manuscript;

                if(!String.IsNullOrEmpty(window.Corrector))
                    manuscript.Signifier = window.Corrector;

                reading.Manuscripts.Add(manuscript);
                variant.Readings.Add(reading);

                Verse.Variants.Add(variant);

                Render();
            }
        }

    }
}
